package ee.subjects.backend.services;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import ee.subjects.backend.database.ConnectionManager;
import ee.subjects.backend.model.dao.Enterprise_person_relation_DAO;
import ee.subjects.backend.model.dao.factory.DAOFactory;
import ee.subjects.backend.model.data.Ent_per_relation_type;
import ee.subjects.backend.model.data.Enterprise_person_relation;

public class Enterprise_person_relation_ServiceImpl implements Enterprise_person_relation_Service
{
	Logger logger = Logger.getLogger(Enterprise_person_relation.class);
	DAOFactory daoFactory = DAOFactory.getDAOFactory(); //PEAB ILMTINGIMATA ENNE ConnectionManageri olema SEST INITSIALISEERIB PROPERTIES ANDMED ConnectionManageri jaoks
	
	
	@Override
	public Enterprise_person_relation addNewRelationWithPerson(
			int enterpriseId, int personId,
			int enterprise_person_relation_typeId)
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		setAutoCommitToFalse(dbConnection);
		Enterprise_person_relation_DAO enterprise_person_relationDAOImpl = 
				daoFactory.getEnterprise_person_relation_DAOImpl(dbConnection);
		Enterprise_person_relation enterprise_person_relation = null;
		
		try 
		{
			enterprise_person_relation = new Enterprise_person_relation();
			enterprise_person_relation.setPerson_fk(personId);
			enterprise_person_relation.setEnterprise_fk(enterpriseId);
			enterprise_person_relation.setEnt_per_relation_type_fk(enterprise_person_relation_typeId);
			
			enterprise_person_relationDAOImpl.insertEnterprise_person_relation(enterprise_person_relation);
			//Lisame enterprise_person_relation objektile ka ent_per_relation_type objekti kaasa
			Ent_per_relation_type ent_per_relation_type = 
					enterprise_person_relationDAOImpl.getEnt_per_relation_typeById(enterprise_person_relation_typeId);
			enterprise_person_relation.setEnterprise_person_relation_type(ent_per_relation_type);
			dbConnection.commit();
		}
		catch (Exception e) 
		{
			rollBackATransaction(dbConnection);
			e.printStackTrace();
		}
		
		return enterprise_person_relation;
	}

	@Override
	public Enterprise_person_relation getEnterprise_person_relationById(
			int enterprise_person_relationId) 
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		Enterprise_person_relation_DAO enterprise_person_relationDAOImpl = 
				daoFactory.getEnterprise_person_relation_DAOImpl(dbConnection);
		Enterprise_person_relation enterprise_person_relation = null;
		
		try
		{
			enterprise_person_relation = enterprise_person_relationDAOImpl.getEnterprise_person_relationById(enterprise_person_relationId);
			//lisame ka ent_per_relation_type objekti
			Ent_per_relation_type ent_per_relation_type = 
					enterprise_person_relationDAOImpl.getEnt_per_relation_typeById(enterprise_person_relation.getEnt_per_relation_type_fk());
			enterprise_person_relation.setEnterprise_person_relation_type(ent_per_relation_type);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return enterprise_person_relation;
	}

	@Override
	public List<Enterprise_person_relation> getAllSpecificEnterpriseAndSpecificPersonRelations(
			int enterpriseId, int personId) 
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		Enterprise_person_relation_DAO enterprise_person_relationDAOImpl = 
				daoFactory.getEnterprise_person_relation_DAOImpl(dbConnection);
		List<Enterprise_person_relation> listOfEnterprise_person_relations = new ArrayList<Enterprise_person_relation>();
		
		try
		{
			listOfEnterprise_person_relations = 
					enterprise_person_relationDAOImpl.getAllSpecificEnterpriseAndSpecificPersonRelations(enterpriseId, personId);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return listOfEnterprise_person_relations;
	}

	@Override
	public List<Enterprise_person_relation> getAllEnterprisePersonRelationsThatSpecificEnterpriseHas(
			int enterpriseId) 
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		Enterprise_person_relation_DAO enterprise_person_relationDAOImpl = 
				daoFactory.getEnterprise_person_relation_DAOImpl(dbConnection);
		List<Enterprise_person_relation> listOfEnterprise_person_relations = new ArrayList<Enterprise_person_relation>();
		
		try
		{
			listOfEnterprise_person_relations = 
					enterprise_person_relationDAOImpl.getAllEnterprisePersonRelationsThatSpecificEnterpriseHas(enterpriseId);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return listOfEnterprise_person_relations;
	}

	@Override
	public List<Enterprise_person_relation> getAllEnterprisePersonRelationsThatSpecificPersonHas(
			int personId) 
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		Enterprise_person_relation_DAO enterprise_person_relationDAOImpl = 
				daoFactory.getEnterprise_person_relation_DAOImpl(dbConnection);
		List<Enterprise_person_relation> listOfEnterprise_person_relations = new ArrayList<Enterprise_person_relation>();
		
		try
		{
			listOfEnterprise_person_relations = 
					enterprise_person_relationDAOImpl.getAllEnterprisePersonRelationsThatSpecificPersonHas(personId);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return listOfEnterprise_person_relations;
	}

	@Override
	public boolean updateEnterprise_person_relation(
			Enterprise_person_relation updatedEnterprise_person_relationObject)
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		setAutoCommitToFalse(dbConnection);
		Enterprise_person_relation_DAO enterprise_person_relationDAOImpl = 
				daoFactory.getEnterprise_person_relation_DAOImpl(dbConnection);
		boolean isUpdateSuccessful = false;
		
		try 
		{
			isUpdateSuccessful = enterprise_person_relationDAOImpl.updateEnterprise_person_relation(updatedEnterprise_person_relationObject);
			dbConnection.commit();
			if (isUpdateSuccessful)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		catch (Exception e) 
		{
			rollBackATransaction(dbConnection);
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean deleteEnterprise_person_relationById(int ent_per_relationId)
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		setAutoCommitToFalse(dbConnection);
		Enterprise_person_relation_DAO enterprise_person_relationDAOImpl = 
				daoFactory.getEnterprise_person_relation_DAOImpl(dbConnection);
		boolean isDeletionSuccessful = false;
		
		try 
		{
			isDeletionSuccessful = enterprise_person_relationDAOImpl.deleteEnterprise_person_relationById(ent_per_relationId);
			dbConnection.commit();
			if (isDeletionSuccessful)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		catch (Exception e) 
		{
			rollBackATransaction(dbConnection);
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean deleteAllEnterprisePersonRelationsByEnterpriseId(
			int enterpriseId) 
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		setAutoCommitToFalse(dbConnection);
		Enterprise_person_relation_DAO enterprise_person_relationDAOImpl = 
				daoFactory.getEnterprise_person_relation_DAOImpl(dbConnection);
		boolean isDeletionSuccessful = false;
		
		try 
		{
			isDeletionSuccessful = enterprise_person_relationDAOImpl.deleteAllEnterprisePersonRelationsByEnterpriseId(enterpriseId);
			dbConnection.commit();
			if (isDeletionSuccessful)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		catch (Exception e) 
		{
			rollBackATransaction(dbConnection);
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public Ent_per_relation_type getEnt_per_relation_typeById(
			int ent_per_relation_typeId) 
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		Enterprise_person_relation_DAO enterprise_person_relationDAOImpl = 
				daoFactory.getEnterprise_person_relation_DAOImpl(dbConnection);
		Ent_per_relation_type ent_per_relation_type = null;
		
		try
		{
			ent_per_relation_type = enterprise_person_relationDAOImpl.getEnt_per_relation_typeById(ent_per_relation_typeId);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return ent_per_relation_type;
	}

	@Override
	public List<Ent_per_relation_type> getAllEnterprise_person_relation_types() 
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		Enterprise_person_relation_DAO enterprise_person_relationDAOImpl = 
				daoFactory.getEnterprise_person_relation_DAOImpl(dbConnection);
		List<Ent_per_relation_type> listOfEnt_per_relation_types = new ArrayList<Ent_per_relation_type>();
		
		try
		{
			listOfEnt_per_relation_types = 
					enterprise_person_relationDAOImpl.getAllEnterprise_person_relation_types();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return listOfEnt_per_relation_types;
	}
	
	//----------HELPER METHODS-----------------
	private void setAutoCommitToFalse(Connection databaseConnection)
	{
		try 
		{
			databaseConnection.setAutoCommit(false);
			logger.info("Autocommit is now set to false");
		} 
		catch (SQLException e) 
		{
			logger.error(e.getMessage());
		}
	}
			
	private void rollBackATransaction(Connection databaseConnection)
	{
		try 
		{
			databaseConnection.rollback();
			logger.info("Rollback has been done!");
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}
		
}
